package com.fjw.dao.impl;

import com.fjw.dao.StudentDao;
import com.fjw.entity.Student;
import com.fjw.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class StudentDaoImpl  implements StudentDao {
    private QueryRunner qr = DBUtils.DB.qr;
    @Override
    public List<Student> selectAllList() throws SQLException {
        String sql ="select * from student";
        List<Student> studentList = qr.query(sql, new BeanListHandler<Student>(Student.class));
        return studentList;
    }

    @Override
    public Student selectBySid(Integer sid) throws SQLException {
        String sql ="select *  from student where sid =?";
        Student student = qr.query(sql, new BeanHandler<Student>(Student.class), sid);
        return student;
    }

    private  Integer sid;
    private  String sname;
    private  String sdesc;
    private  Integer sage;
    @Override
    public int updateStudent(Student student) throws SQLException {
        String sql ="update student set sname=?,sdesc=?,sage=? where sid =?";
        Object [] objects={student.getSname(),student.getSdesc(),student.getSage(),student.getSid()};
        int num = qr.update(sql, objects);
        return num;
    }

    @Override
    public int deleteBySid(Integer sid) throws SQLException {
        String  sql="delete from student where sid=?";
        int num = qr.update(sql, sid);
        return num;
    }

    @Override
    public int addStudent(Student student) throws SQLException {
        String sql ="insert into student(sname,sdesc,sage)values(?,?,?)";
        Object [] objects ={student.getSname(),student.getSdesc(),student.getSage()};
        int num = qr.update(sql, objects);
        return num;
    }
}
